package com.onpositive.gamemaker.swt;

import java.io.InputStream;
import java.util.HashMap;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

import com.onpositive.gamemaker.test.GCCreateFrom;

public class SWTImageManager
{
	private static final RGB WHITE_RGB = new RGB(255,255,255);
	private static HashMap<String, Image> imageMap = new HashMap<String, Image>();
	
	public static Image getImage(String id)
	{
		Image image = imageMap.get(id);
		if (image == null)
		{
			image = new Image(Display.getDefault(), getResourceStreamById(id));
			imageMap.put(id, image);
		}
		return image;
	}
	
	public static Image getImage(String id, boolean transparent)
	{
		if (!transparent)
			return getImage(id);
		else
		{
			Image image = imageMap.get(id);
			if (image == null)
			{
				ImageData data = new ImageData(
						getResourceStreamById(id));
				int whitePixel = data.palette.getPixel(WHITE_RGB);
				data.transparentPixel = whitePixel;
				image = new Image(Display.getDefault(),data);
				imageMap.put(id, image);
			}
			return image;
		}
	}

	protected static InputStream getResourceStreamById(String id)
	{
		return GCCreateFrom.class.getResourceAsStream(id);
	}
}
